package com.lz.autotest.project.testmanage.autoTest.controller;

import com.lz.autotest.common.constant.ReportSourceConstant;
import com.lz.autotest.common.entity.ResponseVo;
import com.lz.autotest.common.utils.GlobalMap;
import com.lz.autotest.common.utils.SecurityUtils;
import com.lz.autotest.common.web.domain.AjaxResult;
import com.lz.autotest.project.testmanage.autoTest.entity.InfResultVo;
import com.lz.autotest.project.testmanage.autoTest.entity.ResultView;
import com.lz.autotest.project.testmanage.autoTest.entity.TsstAutotestMenu;
import com.lz.autotest.project.testmanage.autoTest.service.Engine.XtsEngine;
import com.lz.autotest.project.testmanage.autoTest.service.TsstAutotestMenuService;
import com.lz.autotest.project.testmanage.reportmanage.service.InfReportService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: X
 * @date: Created in 2023/6/14 16:55
 * @Description:
 */
@RestController
@RequestMapping(value = "/TsstAutotestMenu")
public class TsstTestMenuController {

    @Resource
    private TsstAutotestMenuService txtsTestMenuService;
    @Resource
    private InfReportService infReportService;


    private static final Integer lock = 0;

    /**
     * 新增
     *
     * @author zip.li
     * @date 2023/06/14
     **/
    @RequestMapping("/insert")
    @PreAuthorize("@ss.hasPermi('testmanage:autoTest:insert')")
    public Object insert(@RequestBody TsstAutotestMenu tsstAutotestMenu) {
        tsstAutotestMenu.setCreateBy(SecurityUtils.getUsername());
        return txtsTestMenuService.insert(tsstAutotestMenu);
    }

    /**
     * 刪除
     *
     * @author zip.li
     * @date 2023/06/14
     **/
    @RequestMapping("/delete")
    @PreAuthorize("@ss.hasPermi('testmanage:autoTest:delete')")
    public ResponseVo delete(@RequestParam Map<String, Object> param) {
        return txtsTestMenuService.delete(param);
    }

    /**
     * 更新
     *
     * @author zip.li
     * @date 2023/06/14
     **/
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermi('testmanage:autoTest:update')")
    public ResponseVo update(@RequestBody TsstAutotestMenu tsstAutotestMenu) {
        tsstAutotestMenu.setUpdateBy(SecurityUtils.getUsername());
        return txtsTestMenuService.update(tsstAutotestMenu);
    }

    /**
     * 查询 根据主键 id 查询
     *
     * @author zip.li
     * @date 2023/06/14
     **/
    @RequestMapping("/load")
    public Object load(int id) {
        return txtsTestMenuService.load(id);
    }

    /**
     * 获取接口下拉树列表
     */
    @GetMapping("/treeselect")
    public AjaxResult treeselect(@RequestParam Map<String, Object> param) {
        List<TsstAutotestMenu> tsstTestMenus = txtsTestMenuService.selectAllMenu(param);
        return AjaxResult.success(txtsTestMenuService.buildSstInfTreeSelect(tsstTestMenus));
    }

    /**
     * 获取接口下拉树列表
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('testmanage:autoTest:list')")
    public AjaxResult selectTsstAutoTestMenuList(@RequestParam Map<String, Object> param) {
        List<TsstAutotestMenu> tsstTestMenus = txtsTestMenuService.selectAllMenu(param);
        return AjaxResult.success(tsstTestMenus);
    }

    /**
     * 自动化测试
     *
     * @author zip.li
     * @date 2023/06/20
     **/
    @PostMapping("/autoTestRun.action")
    @PreAuthorize("@ss.hasPermi('testmanage:autoTest:run')")
    public ResponseVo autoTestRun(@RequestBody TsstAutotestMenu tsstAutotestMenu) {
        tsstAutotestMenu.setReportSource(ReportSourceConstant.AUTO_TEST);
        return txtsTestMenuService.autoTestRun(tsstAutotestMenu);
    }

    /**
     * @return
     * @Description: 前端定时获取测试进度
     * @author: X
     * @DateTime: 2023/9/25 9:56
     **/
    @GetMapping("/queryInfResultTimer/{reportId}")
    public ResponseVo queryInfResultTimer(@PathVariable String reportId) {
        ArrayList<ResultView> res = null;
        InfResultVo resultVo = new InfResultVo();
        synchronized (lock) {
            InfResultVo result = GlobalMap.getInfResult(reportId);
            List<ResultView> infResult = result.getList();
            res = new ArrayList<>(infResult);
            infResult.clear();
            resultVo.setIsDone(result.getIsDone());
            resultVo.setTotalTime(result.getTotalTime());
            resultVo.setList(res);
        }
        return ResponseVo.success(resultVo);
    }

    /**
     * @param reportId
     * @return
     * @Description: 停止记录接口结果，当用户离开过程中调用
     * @author: X
     * @DateTime: 2023/9/25 14:19
     **/
    @GetMapping("/stopRecordInfresult/{reportId}")
    public void stopRecordInfresult(@PathVariable String reportId) {
        GlobalMap.updateInfResult(reportId, 0, null, null);
    }

    /**
     * @description 停止测试任务
     * @author zip.li
     * @date 2023/9/1 15:43
     * @Version1.0
     **/
    @GetMapping("/stopRunTask.action/{taskId}")
    public ResponseVo stopRunTask(@PathVariable String taskId) {
        XtsEngine getengine = GlobalMap.getengine(taskId);
        if (getengine == null) {
            return ResponseVo.success("已终止");
        }
        //更新状态
        GlobalMap.updateInfResult(taskId, 0, 0, null);
        getengine.stopTest();
        GlobalMap.removeEngine(taskId);
        Map<String, Object> map = new HashMap<>();
        map.put("reportId", taskId);
        map.put("status", "1");
        infReportService.updateTsstReport(map);
        return ResponseVo.success("终止成功");
    }
}